package com.pansoft.openplanet.fragment;

import android.os.Bundle;
import com.google.android.material.tabs.TabLayout;
import androidx.viewpager.widget.ViewPager;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import com.efounder.chat.http.JFCommonRequestManager;
import com.efounder.frame.baseui.BaseFragment;
import com.efounder.util.AppContext;
import com.efounder.util.ToastUtil;
import com.efounder.utils.ResStringUtil;
import com.google.gson.Gson;
import com.pansoft.kline.helper.BtcKanConfigAndRateHelper;
import com.pansoft.kline.model.MarketMenuBean;
import com.pansoft.openplanet.R;
import com.pansoft.openplanet.adapter.BtckanFragmentAdapter;
import com.utilcode.util.SPUtils;

import org.json.JSONObject;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

import static com.pansoft.openplanet.constant.MarketConstant.KEY_MARKET_CATEGORY_VERSION;
import static com.pansoft.openplanet.constant.MarketConstant.KEY_MARKET_MENU;
import static com.pansoft.openplanet.constant.MarketConstant.MARKET_MENU_URL;

/**
 * 行情菜单界面
 *
 * @author cimu
 * @date 2018/11/7
 */

public class BtckanFragment extends BaseFragment {
    private static final String TAG = "BtckanFragment";
    private View mView;
    private TabLayout tabBtckan;
    private ViewPager viewpagerBtckan;
    private BtckanFragmentAdapter mBtckanFragmentAdapter;
    private List<MarketMenuBean.DataBean.CategoryBean.ListBean> coinMenuList = new ArrayList<>();

    /**
     * 交易所汇率
     */
    public static JSONObject rateObject;

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        mView = LayoutInflater.from(getActivity()).inflate(R.layout.assets_fragment_btckan, container, false);
        initView();
        getRateData();
        initMenuData();
        return mView;
    }


    private void initView() {
        tabBtckan = mView.findViewById(R.id.tab_btckan);
        viewpagerBtckan = mView.findViewById(R.id.viewpager_btckan);
        mBtckanFragmentAdapter = new BtckanFragmentAdapter(getChildFragmentManager(), coinMenuList);

        viewpagerBtckan.setAdapter(mBtckanFragmentAdapter);
        tabBtckan.setupWithViewPager(viewpagerBtckan);
    }

    /**
     * 初始化菜单数据
     */
    private void initMenuData() {
        HashMap<String, String> hashMap = new HashMap<>();
        hashMap.put("category_version", "");
        JFCommonRequestManager.getInstance(getActivity()).requestGetByAsyn(TAG, MARKET_MENU_URL, hashMap, new JFCommonRequestManager.ReqCallBack<String>() {
            @Override
            public void onReqSuccess(String result) {
                try {
                    SPUtils.getInstance().put(KEY_MARKET_MENU, result);
                    setData(result);
                } catch (Exception e) {
                    e.printStackTrace();
                    ToastUtil.showToast(AppContext.getInstance(), R.string.common_text_http_request_fail);

                }
            }

            @Override
            public void onReqFailed(String errorMsg) {
                ToastUtil.showToast(AppContext.getInstance(), R.string.common_text_http_request_fail_abnormal);
                try {
                    String json = SPUtils.getInstance().getString(KEY_MARKET_MENU);
                    setData(json);
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        });


    }

    private void setData(String result) {
        MarketMenuBean marketMenuBean = new Gson().fromJson(result, MarketMenuBean.class);
        if (marketMenuBean.getCode() != 0) {
            ToastUtil.showToast(AppContext.getInstance(), R.string.common_text_http_request_fail);
            return;
        }
        SPUtils.getInstance().put(KEY_MARKET_CATEGORY_VERSION,marketMenuBean.getData().getCategory().getVersion());
        List<MarketMenuBean.DataBean.CategoryBean.ListBean> titleList = marketMenuBean.getData().getCategory().getList();
        inintMenu(titleList);

    }

    private void inintMenu(List<MarketMenuBean.DataBean.CategoryBean.ListBean> titleList) {
        coinMenuList.clear();
        for (int i = 0; i < 4; i++) {
            MarketMenuBean.DataBean.CategoryBean.ListBean titleBean = new MarketMenuBean.DataBean.CategoryBean.ListBean();
            if (i == 0) {
                titleBean.setTitle(ResStringUtil.getString(R.string.open_planet_btckan_menu1));
            } else if (i == 1) {
                titleBean.setTitle(ResStringUtil.getString(R.string.open_planet_btckan_menu2));
            } else if (i == 2) {
                titleBean.setTitle(ResStringUtil.getString(R.string.open_planet_btckan_menu3));
            } else if (i == 3) {
                titleBean.setTitle(ResStringUtil.getString(R.string.open_planet_btckan_menu4));
            }
            coinMenuList.add(i, titleBean);
        }
        coinMenuList.addAll(titleList);
        mBtckanFragmentAdapter.notifyDataSetChanged();
    }


    /**
     * 请求各个交易所汇率信息
     */
    private void getRateData() {
        BtcKanConfigAndRateHelper.getRate(null);
    }
}
